<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <div class="logo-section">
          <img src="@/assets/home_logo.jpg" alt="Logo" class="logo" />
        </div>
        <h1 class="title">新华冶金智慧安防平台</h1>
        <p class="subtitle">XINHUA METALLURGICAL SMART SECURITY PLATFORM</p>
      </div>
      
      <div class="login-form-container">
        <div class="form-title">用户登录</div>
        <div class="form-divider"></div>
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @keyup.enter="handleLogin"
        >
          <el-form-item prop="name">
            <el-input
              v-model="loginForm.name"
              placeholder=""
              size="large"
              :prefix-icon="User"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder=""
              size="large"
              :prefix-icon="Lock"
              show-password
              autocomplete="new-password"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item class="remember-item">
            <div class="login-options">
              <el-checkbox v-model="loginForm.remember">记住我 (公共场合请勿)</el-checkbox>
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    
    <div class="login-footer">
      <p>Copyright ©2014-2023 新华冶金智慧安防平台 V1.0.250901</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElForm, ElFormItem, ElInput, ElButton, ElCheckbox } from 'element-plus'
import { success, info } from '@/utils/toast'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'
import { getSavedLoginInfo } from '@/utils/auth'
import type { LoginForm } from '@/types'

// Store
const authStore = useAuthStore()

// 响应式数据
const loginFormRef = ref<InstanceType<typeof ElForm>>()
const loading = ref(false)

const loginForm = reactive<LoginForm>({
  name: '',
  password: '',
  remember: false,
  loginName: '',
  loginPassword: ''
})

// 表单验证规则
const loginRules = {
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度为 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 20, message: '密码长度为 3 to 20 个字符', trigger: 'blur' }
  ]
}


// 方法
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  const valid = await loginFormRef.value.validate().catch(() => false)
  if (!valid) return
  
  try {
    loading.value = true
    await authStore.login(loginForm)
    success('登录成功')
  } finally {
    loading.value = false
  }
}


// 页面初始化
onMounted(() => {
  // 如果已经登录，跳转到首页
  if (authStore.isLoggedIn) {
    info('您已经登录，正在跳转到首页...')
    return
  }
  
  // 检查是否有保存的登录信息
  const savedLoginInfo = getSavedLoginInfo()
  if (savedLoginInfo) {
    loginForm.name = savedLoginInfo.username
    loginForm.password = savedLoginInfo.password
    loginForm.remember = savedLoginInfo.remember
  }
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background-image: url('@/assets/logo.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(5px);
  z-index: 1;
}

.login-content {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 800px;
  padding: 20px;
}

.login-header {
  text-align: center;
  margin-bottom: 60px;
  color: white;
}

.logo-section {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
  gap: 15px;
}

.logo {
  /* width: 48px; */
  /* height: 48px; */
  background: #e74c3c;
  border-radius: 4px;
  object-fit: cover;
}

.company-name {
  color: #e74c3c;
  font-size: 24px;
  font-weight: bold;
}

.title {
  color: white;
  font-size: 28px;
  font-weight: bold;
  margin: 0 0 15px 0;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  margin: 0;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.login-form-container {
  width: 100%;
  max-width: 320px;
}

.form-title {
  color: white;
  font-size: 14px;
  margin-bottom: 8px;
  text-align: left;
}

.form-divider {
  width: 100%;
  height: 1px;
  background: rgba(255, 255, 255, 0.3);
  margin-bottom: 15px;
}

.login-form {
  background: transparent;
}

.custom-input {
  margin-bottom: 10px;
  height: 32px;
}

.custom-input :deep(.el-input__wrapper) {
  background: #ffffff !important;
  border-radius: 4px;
  border: 1px solid #ddd;
  box-shadow: none !important;
  height: 32px !important;
}

.custom-input :deep(.el-input__inner) {
  color: #333 !important;
  font-size: 14px;
  background: #ffffff !important;
  height: 30px !important;
  line-height: 30px !important;
}

.custom-input :deep(.el-input__inner)::-webkit-credentials-auto-fill-button {
  display: none !important;
  visibility: hidden !important;
}

.custom-input :deep(.el-input__inner)::-webkit-autofill,
.custom-input :deep(.el-input__inner)::-webkit-autofill:hover,
.custom-input :deep(.el-input__inner)::-webkit-autofill:focus,
.custom-input :deep(.el-input__inner)::-webkit-autofill:active {
  -webkit-box-shadow: 0 0 0 1000px #ffffff inset !important;
  -webkit-text-fill-color: #333 !important;
  background-color: #ffffff !important;
  background-image: none !important;
  transition: background-color 5000s ease-in-out 0s;
  height: 30px !important;
}

.custom-input :deep(.el-input__prefix) {
  color: #666;
}

.remember-item {
  margin-bottom: 8px !important;
}

.remember-item :deep(.el-form-item__content) {
  margin-bottom: 0 !important;
}

.login-options {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  margin: 0;
  padding: 0;
}

.login-options :deep(.el-checkbox) {
  color: white;
}

.login-options :deep(.el-checkbox__label) {
  color: white;
  font-size: 14px;
}

.login-btn {
  width: 100%;
  height: 32px;
  font-size: 16px;
  border-radius: 4px;
  background: #007bff;
  border-color: #007bff;
  font-weight: 500;
}

.login-btn:hover {
  background: #0056b3;
  border-color: #0056b3;
}

.login-footer {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  z-index: 2;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .title {
    font-size: 24px;
  }
  
  .subtitle {
    font-size: 14px;
  }
  
  .logo-section {
    gap: 10px;
  }
  
  .company-name {
    font-size: 20px;
  }
  
  .login-form-container {
    max-width: 280px;
  }
}

@media (max-width: 880px) {
  .login-content {
    padding: 15px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .subtitle {
    font-size: 12px;
  }
  
  .login-form-container {
    max-width: 400px;
  }
  
  .login-footer {
    font-size: 12px;
    bottom: 20px;
  }
}

/* 动画效果 */
.login-content {
  animation: fadeInUp 0.8s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>